#!/bin/bash
#
# OCI-based mktree backend

set -e

PROGRAM=$(basename $0)
if [ "$PROGRAM" == "mktree" ]; then
    # Running from build directory
    CMAKE=yes
    NATIVE=@MKTREE_NATIVE@
    PODMAN=@PODMAN@
    CONTEXT=@CMAKE_SOURCE_DIR@
else
    # Running from source directory
    CMAKE=no
    NATIVE=no
    PODMAN=${MKTREE_ENGINE:-podman}
    CONTEXT=..
fi

if [ $NATIVE == yes ]; then
    FROM="--from @OS_NAME@:@OS_VERSION@"
else
    FROM=
fi

DEST_DIR="$PWD/mktree.destdir"
CACHE_DIR="mktree.cache"
IID_FILE="$CACHE_DIR/image-id"
IMAGE_ID=$(cat $IID_FILE 2>/dev/null || echo "")
BASE_TAG="rpm/base:$(sha256sum Dockerfile | head -c8)"
ARGS="-f Dockerfile $FROM $CONTEXT"
ROOTLESS=$([ $(id -u) == 0 ] && echo 0 || echo 1)
CMD=$1; shift

export ROOTLESS
source mktree.common

mkdir -p $CACHE_DIR

rpmtests()
{
    local opts
    local vol="$PWD:/srv:z"

    # Pass select options to $PODMAN
    case "$1" in
        # Useful for CI environments without a tty
        --interactive=*) opts=$1; shift ;;
    esac

    # Don't keep the test artifacts around in non-cmake mode
    if [ $CMAKE == no ]; then
        set -- --log "$@"
        vol=/srv
    fi

    $PODMAN run --privileged -it --rm --read-only --tmpfs /tmp -v $vol \
                --workdir /srv -e ROOTLESS=$ROOTLESS $opts $IMAGE_ID \
                rpmtests "$@"
}

clean()
{
    rm -f "$IID_FILE"
    [ -z "$IMAGE_ID" ] || [ -z "$($PODMAN images -q $IMAGE_ID)" ] && return
    [ "$($PODMAN inspect -f "{{ .RepoTags }}" $IMAGE_ID)" != "[]" ] && return
    $PODMAN rmi $IMAGE_ID >/dev/null
}

case $CMD in
    build)
        # Build base image
        $PODMAN build --target base -t $BASE_TAG $ARGS

        # Add RPM install on top
        clean
        if [ $NATIVE == yes ]; then
            # Native build
            id=$($PODMAN create -t $BASE_TAG)
            trap "$PODMAN kill $id >/dev/null; $PODMAN rm $id > /dev/null" EXIT
            $PODMAN start $id
            rm -rf "$DEST_DIR"
            make_install $DEST_DIR
            $PODMAN cp $DEST_DIR/. $id:/
            $PODMAN exec $id /usr/share/mktree/setup.sh
            $PODMAN commit -q $id --iidfile=$IID_FILE
        else
            # Standalone build
            $PODMAN build --target full --iidfile=$IID_FILE $ARGS
        fi
    ;;
    check)
        rpmtests "$@"
    ;;
    atshell)
        rpmtests --shell "$@"
    ;;
    shell)
        rpmtests --shell snapshot shell
    ;;
    reset)
        rpmtests --reset
    ;;
    tag)
        $PODMAN tag $IMAGE_ID $1
    ;;
    clean)
        clean
    ;;
esac
